/**
 * 
 */
package info.niwota.fwrite;

import android.app.TabActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.ImageButton;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;

/**
 * @author qiangli
 * 
 */
public class MainActivity extends TabActivity implements OnClickListener {

	private static final String TAG = "MainActivity";

	public static final String EXTRA_TAB = "extra.tab";

	public static final String EXTRA_NOBACK = "extra.noback";

	private static final int[] TABS = new int[] { -1, R.id.tab_today,
			R.id.tab_day, R.id.tab_week, R.id.tab_month, R.id.tab_all };

	private static final Class<?>[] CLASSES = new Class[] { null,
			TabTodayActivity.class, TabDayActivity.class,
			TabWeekActivity.class, TabMonthActivity.class, TabAllActivity.class };

	private static final int DEFAULT_TAB = 1;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		setFullScreen();
		
		super.onCreate(savedInstanceState);

		if (Debug.DEBUG) {
			Log.d(TAG, "onCreate " + savedInstanceState);
		}
		//
		setContentView(R.layout.main);

		// init tabs
		final TabHost tabHost = getTabHost();

		//
		String tab0 = getString(R.string.tab_home);
		TabHost.TabSpec spec = tabHost.newTabSpec(tab0)
				.setIndicator(tab0);
		spec.setContent(R.id.tab_home);
		tabHost.addTab(spec);

		//

		for (int i = 1; i < TABS.length; i++) {
			final Intent intent = new Intent().setClass(this, CLASSES[i]);
			intent.putExtra(EXTRA_NOBACK, true);
			final String tab = getString(TABS[i]);
			spec = tabHost.newTabSpec(tab).setIndicator(tab);
			spec.setContent(intent);
			tabHost.addTab(spec);
		}

		// init
		initRes();

		//
		int tab = getIntent().getIntExtra(EXTRA_TAB, DEFAULT_TAB);
		
		setTab(tab);

		//
		tabHost.setOnTabChangedListener(new OnTabChangeListener() {
			public void onTabChanged(String tabId) {
				hideKeyboard(tabHost);
			}
		});

		if (Debug.DEBUG) {
			Log.d(TAG, "onCreate done.");
		}
		//
		addListeners();

	}

	private void initRes() {

	}

	private void hideKeyboard(View v) {
		InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
		imm.hideSoftInputFromWindow(v.getApplicationWindowToken(), 0);
	}

	private void setFullScreen() {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
	}
	
	private void setTab(int tab) {
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		//
		getTabHost().setCurrentTab(tab);
		for (int i = 1; i < TABS.length; i++) {
			ImageButton bt = (ImageButton) findViewById(TABS[i]);
			final int imgred = (i == 1 ? R.drawable.circle_red12 : R.drawable.square_red12);
			final int imgblue = (i == 1 ? R.drawable.circle_blue12 : R.drawable.square_blue12);
			int d = (i == tab ? imgred : imgblue);
			bt.setImageResource(d);
		}
	}

	private void addListeners() {
		for (int i = 1; i < TABS.length; i++) {
			View bt = findViewById(TABS[i]);
			bt.setOnClickListener(this);
		}
	}

	public void onClick(View v) {
		final int id = v.getId();
		for (int i = 0; i < TABS.length; i++) {
			if (id == TABS[i]) {
				setTab(i);
				return;
			}
		}

		//
		switch (id) {
		}
	}

	@Override
	protected void onPause() {
		super.onPause();
		if (Debug.DEBUG) {
			Log.d(TAG, "onPause");
		}
	}

	@Override
	protected void onResume() {
		super.onResume();

		if (Debug.DEBUG) {
			Log.d(TAG, "onResume");
		}
	}

	@Override
	protected void onStart() {
		super.onStart();
		if (Debug.DEBUG) {
			Log.d(TAG, "onStart");
		}
	}

	@Override
	public void onStop() {
		super.onStop();
		
		if (Debug.DEBUG) {
			Log.d(TAG, "onStop");
		}
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		
		if (Debug.DEBUG) {
			Log.d(TAG, "onDestroy");
		}
	}
}
